//
//  TravelAccountsViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/3/31.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TravelAccountsViewController.h"
#import "EditViewController.h"
#import "FMDBModel.h"
#import "AccountModel.h"
#import "Singleton.h"

@interface TravelAccountsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *generalTableView;
@property(nonatomic,strong)UITableView *detailsTableView;
@property(nonatomic,strong)NSMutableArray *generalArray;
@property(nonatomic,strong)NSArray *typeArray;
// 数据库源
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)CGFloat CNY; // 人民币
@property(nonatomic,assign)CGFloat HKD; // 港币
@property(nonatomic,assign)CGFloat USD; // 美元
@property(nonatomic,assign)CGFloat JPY; // 日元
@property(nonatomic,assign)CGFloat TWD; // 台币
@property(nonatomic,assign)CGFloat EUR; // 欧元
@property(nonatomic,assign)CGFloat GBP; // 英镑
@property(nonatomic,assign)CGFloat AUD; // 澳币
@property(nonatomic,assign)CGFloat KRW; // 韩币
@property(nonatomic,assign)CGFloat THB; // 泰铢


@end

@implementation TravelAccountsViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.generalArray = [NSMutableArray array];
    self.typeArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    self.dataArray = [[FMDBModel defaultFMDBModel]getAllAccount];
    
    self.CNY = 0;
    self.HKD = 0;
    self.USD = 0;
    self.JPY = 0;
    self.TWD = 0;
    self.EUR = 0;
    self.GBP = 0;
    self.AUD = 0;
    self.KRW = 0;
    self.THB = 0;
    
    for (AccountModel *model in self.dataArray) {
        if ([model.type isEqualToString:@"CNY 人民币"]) {
            self.CNY += [model.total floatValue];

        } else if([model.type isEqualToString:@"HKD 港币"]) {
            self.HKD += [model.total floatValue];

        } else if([model.type isEqualToString:@"USD 美元"]) {
            self.USD += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"JPY 日元"]) {
            self.JPY += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"TWD 台币"]) {
            self.TWD += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"EUR 欧元"]) {
            self.EUR += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"GBP 英镑"]) {
            self.GBP += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"AUD 澳币"]) {
            self.AUD += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"KRW 韩币"]) {
            self.KRW += [model.total floatValue];
            
        } else if([model.type isEqualToString:@"THB 泰铢"]) {
            self.THB += [model.total floatValue];
        }
    }
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.CNY]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.HKD]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.USD]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.JPY]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.TWD]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.EUR]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.GBP]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.AUD]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.KRW]];
    [self.generalArray addObject:[NSString stringWithFormat:@"%.2f",self.THB]];
    self.typeArray = @[@"人民币",@"港币",@"美元",@"日元",@"台币",@"欧元",@"英镑",@"澳币",@"韩币",@"泰铢"];
    [self.generalTableView reloadData];
    [self.detailsTableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"旅行记账";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addAction)];
    
    self.generalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3) style:UITableViewStyleGrouped];
    self.detailsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 260, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - CGRectGetHeight(self.generalTableView.frame) - 150) style:(UITableViewStyleGrouped)];
    
    UIButton *allDeleteButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    allDeleteButton.frame = CGRectMake(50, CGRectGetMaxY(self.detailsTableView.frame) + 30, self.view.frame.size.width - 100, 30);
    allDeleteButton.backgroundColor = [UIColor redColor];
    [allDeleteButton setTitle:@"清除账本" forState:(UIControlStateNormal)];
    [allDeleteButton addTarget:self action:@selector(allDeleteAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:allDeleteButton];
    
    self.generalTableView.delegate = self;
    self.generalTableView.dataSource = self;
    self.detailsTableView.delegate = self;
    self.detailsTableView.dataSource = self;
    
    [self.view addSubview:self.generalTableView];
    [self.view addSubview:self.detailsTableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.generalTableView]) {
        return 10;
    }else{
        return self.dataArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if ([tableView isEqual:self.generalTableView]) {
        return @"消费总账";
    }else{
        return @"消费明细";
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    if ([tableView isEqual:self.generalTableView]) {
        
        if (self.dataArray != nil ) {
            
            cell.textLabel.text = self.typeArray[indexPath.row];
            cell.detailTextLabel.text = self.generalArray[indexPath.row];
            
        } else {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"暂无消费明细";
            }else{
                cell.textLabel.text = @"查看消费统计";
            }
        }
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        AccountModel *model = [[AccountModel alloc]init];
        model = self.dataArray[indexPath.row];
        
        NSString *type = [model.type substringFromIndex:4];
        
        if ([model.consumption isEqualToString:@""] && [model.consumption isEqual:[NSNull null]]) {
            cell.textLabel.textAlignment = NSTextAlignmentLeft;
            cell.textLabel.font = [UIFont fontWithName:@"" size:14];
            cell.textLabel.text = [NSString stringWithFormat:@"%@: %@ %@",model.category,model.total,type];
        }else {
            cell.textLabel.text = [NSString stringWithFormat:@"%@: %@ %@ (%@)",model.category,model.total,type,model.consumption];
            
        }
        
        NSString *time = [model.time substringWithRange:NSMakeRange(5, 12)];
        cell.detailTextLabel.text = time;
        
    }
    return cell;
}


- (void)addAction
{
    EditViewController *editVC = [[EditViewController alloc]init];
    
    [self.navigationController pushViewController:editVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.detailsTableView]) {
        
        EditViewController *editVC = [[EditViewController alloc]init];
        
        AccountModel *account = [[AccountModel alloc]init];
        account = self.dataArray[indexPath.row];
        
        Singleton *single = [Singleton shareSingle];
        single.address = account.address;
        single.time = account.time;
        single.type = account.type;
        single.total = account.total;
        single.consumption = account.consumption;
        
        [self.navigationController pushViewController:editVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (void)allDeleteAction
{
    [[FMDBModel defaultFMDBModel]deleteAllAccount];
    [self.generalTableView reloadData];
    [self.detailsTableView reloadData];
}

@end
